<?php
class order_model extends MY_model
{
    public $id;
    public $store_id;
    public $table_id;
    public $total;
    public $created_date;
    public $modified_date;

    public function get_elements()
    {
        $query = $this->db->query("SELECT * FROM orders");
        return $this->to_domain_models($query->result());
    }

    public function get_element_by_id($id)
    {
        $query = $this->db->query("SELECT * FROM orders WHERE id = ?",array($id));
        return $this->to_domain_model($query->row());
    }

    public function get_element_by_store_id($id)
    {
        $query = $this->db->query("SELECT * FROM orders WHERE store_id = ?",array($id));
        return $this->to_domain_model($query->row());
    }

    public function add_order($data)
    {
        $this->db->insert('orders',$data);
        if($this->db->affected_rows() > 0)
        {
            return true;
        }
        return false;
    }

    public function delete_by_id($id)
    {
        $this->db->delete('order',array('id' => $id));
        if($this->db->affected_rows() > 0)
        {
            return true;
        }
        return false;
    }

    // -------------------------------------------------------------------------------- PRIVATE PROPERTIES
    public function get_table_name()
    {
        return $this->table_model->get_element_by_id($this->table_id)->name;
    }

    // -------------------------------------------------------------------------------- PRIVATE METHOD

    // Private method
    public function to_domain_model($item)
    {
        $model = new order_model();
        $model->id = isset($item->id) ? $item->id : '' ;
        $model->store_id = isset($item->store_id) ? $item->store_id : '' ;
        $model->total = isset($item->total) ? $item->total : '' ;
        $model->table_id = isset($item->table_id) ? $item->table_id : '' ;
        $model->created_date = isset($item->created_date) ? $item->created_date : '0' ;
        $model->modified_date = isset($item->modified_date) ? $item->modified_date : '0' ;
        return $model;
    }
}